package com.sskj.mine;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.asset.data.CoinTypeBean;
import com.sskj.mine.bean.BuyBalanceBean;
import com.sskj.mine.bean.PayWayListBean;
import com.sskj.mine.bean.ServiceChargeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class ShellActivity extends BaseActivity<ShellPresenter> {
    @BindView(R2.id.btLogin)
    Button btLogin;
    @BindView(R2.id.right_iv)
    ImageView right_iv;
    private List<CoinTypeBean> coinTypeBeans;
    private String type;
    private String pid;
    @BindView(R2.id.tradeTypeSpinner)
    HeySpinner tradeTypeSpinner;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.tv_bank_name)
    TextView tv_bank_name;
    @BindView(R2.id.tv_bank_type)
    TextView tv_bank_type;
    @BindView(R2.id.tv_bank_code)
    TextView tv_bank_code;
    @BindView(R2.id.etInputMoney)
    EditText etInputMoney;
    @BindView(R2.id.tvBalance)
    TextView tvBalance;
    @BindView(R2.id.tvLeftMoney)
    TextView tvLeftMoney;
    @BindView(R2.id.tvServiceCharge)
    TextView tvServiceCharge;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    private String bankid;
    private String banktp;
    private ServiceChargeBean data;
    private Disposable timeDis;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shell;
    }

    @Override
    public ShellPresenter getPresenter() {
        return new ShellPresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ShellActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_detailsActivity2_j));
    }

    @Override
    protected void initData() {
        mPresenter.getAllCoin(false);
        mPresenter.requsetPayWayList();
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(right_iv, () -> {
            Intent intent = new Intent(this, BuyShellRecordActivity.class);
            intent.putExtra("type", "2");
            startActivity(intent);
        });
        ClickUtil.click(btLogin, () -> {
            if (TextUtils.isEmpty(type)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity2_j));
                return;
            }
            if (TextUtils.isEmpty(etInputMoney.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity3_j));
                return;
            }
            if (Double.compare(Double.parseDouble(etInputMoney.getText().toString().trim()), Double.parseDouble(data.getMax())) > 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity4_j));
                return;
            }
            if (TextUtils.isEmpty(etPwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity5_j));
                return;
            }
            if (TextUtils.isEmpty(etCode.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_withdrawActivity6_j));
                return;
            }
            mPresenter.Shell(etInputMoney.getText().toString().trim(), pid, bankid, banktp, etPwd.getText().toString().trim(), etCode.getText().toString());
        });
        ClickUtil.click(tvGetCheckCode, () -> {
            tvGetCheckCode.setEnabled(false);
            mPresenter.sendCode();
        });
    }

    public void updatePayUi(List<PayWayListBean> data) {
        for (PayWayListBean c : data) {
            if (c.type.equals("bankcard")) {
                tv_bank_code.setText(c.number);
                tv_bank_name.setText(c.name);
                tv_bank_type.setText(c.tip);
                bankid = c.number;
                banktp = c.tip;
            }
        }
    }

    public void updateBuyUi() {
        etInputMoney.setText("");
        Intent intent = new Intent(this, BuyShellRecordActivity.class);
        intent.putExtra("type", "2");
        startActivity(intent);
    }

    public void updateServiceCharge(ServiceChargeBean serviceChargeBean) {
        this.data = serviceChargeBean;
        tvServiceCharge.setText(App.INSTANCE.getString(R.string.mine_securityActivity89_j) + "    " + serviceChargeBean.getFree() + type);
        tvBalance.setText(App.INSTANCE.getString(R.string.mine_securityActivity88_j) + "    " + serviceChargeBean.getMax() + type);
    }

    public void updateCoinType(List<CoinTypeBean> data) {
        this.coinTypeBeans = data;
        ArrayList<String> items = new ArrayList<>();
//        items.add(App.INSTANCE.getString(R.string.mine_shellActivity8_j));
        for (CoinTypeBean c : data) {
            items.add(c.getPname());
        }
        tradeTypeSpinner.setItemGravity(Gravity.CENTER);
        tradeTypeSpinner.attachData(items);
        type = coinTypeBeans.get(0).getPname();
        pid = coinTypeBeans.get(0).getPid();
//        mPresenter.getBalance(coinTypeBeans.get(0).getMark());
        mPresenter.requsetServiceCharge(coinTypeBeans.get(0).getPid());
        mPresenter.getMyMoney(type);
        tvType.setText(type);
        tradeTypeSpinner.setOnSelectListener(new HeySpinner.OnSelectListener() {
            @Override
            public void onSelect(int position) {

                type = coinTypeBeans.get(position).getPname();
                pid = coinTypeBeans.get(position).getPid();
//                mPresenter.getBalance(coinTypeBeans.get(position).getMark());
                tvType.setText(type);

            }
        });
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        tvGetCheckCode.setText(aLong + "s");
                    } else {
                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.mine_withdrawActivity8_j));
                        tvGetCheckCode.setEnabled(true);
                    }
                });
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void updateLeftMoney(String usable) {
        tvLeftMoney.setText(getString(R.string.mine_balance)+"    " + usable + type);

    }
}
